package queries;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import connect.DBConnectionUtil;

public class Query6 extends JFrame{
	public Query6(){
		this.setBounds(300,240,500,400);
		this.setLayout(null);
		JLabel label0 = new JLabel("Enter patient infor to update patient contact");
		label0.setBounds(100, 10, 500, 30);
		this.add(label0);
		JLabel label2 = new JLabel("Pid");
		JTextField pid = new JTextField();
		label2.setBounds(100, 50, 100, 30);
		pid.setBounds(200, 50, 200, 30);
		this.add(label2);
		this.add(pid);
		JLabel label1 = new JLabel("Pname:");
		JTextField pname = new JTextField();
		label1.setBounds(100, 100, 100, 30);
		pname.setBounds(200, 100, 200, 30);
		this.add(label1);
		this.add(pname);
		
		JLabel label3 = new JLabel("Address");
		JTextField address = new JTextField();
		label3.setBounds(100, 150, 100, 30);
		address.setBounds(200, 150, 200, 30);
		this.add(label3);
		this.add(address);
		
		JLabel label4 = new JLabel("Phone");
		JTextField phone = new JTextField();
		label4.setBounds(100, 200, 100, 30);
		phone.setBounds(200, 200, 200, 30);
		this.add(label4);
		this.add(phone);
		
		JButton query = new JButton("Update");
		query.setBounds(200, 250, 100, 30);
		this.add(query);
		this.setTitle("Query6:Update patient contact");
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setVisible(true);
		query.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				try{
					Integer.parseInt(pid.getText());
				}catch(Exception e){
					JOptionPane.showMessageDialog(null,
							"Invalid pid!");
					return;
				}
				try {
					// 1.connect database
					Class.forName(DBConnectionUtil.DBDRIVER).newInstance();
					Connection con = DBConnectionUtil.getConnection();
					if(con!=null){
						System.out.println("Connect database successfully!");
					}
					PreparedStatement ps = con
							.prepareStatement("SELECT  * FROM Patient WHERE pid=?");
					ps.setInt(1, Integer.parseInt(pid.getText()));
					ResultSet rset = ps.executeQuery();
					if (!rset.next()) {
						JOptionPane.showMessageDialog(null,
								"Pid does not exsit!");
						return;
					}
					if(pname.getText().trim().equals("")&&address.getText().trim().equals("")&&phone.getText().trim().equals("")){
						JOptionPane.showMessageDialog(null,
								"No infor to be unpated!");
						return;
					}
					// 2.query code
					if(!pname.getText().trim().equals("")){
						ps = con
								.prepareStatement("UPDATE Patient SET pname=? WHERE pid=?");
						ps.setString(1, pname.getText());
						ps.setInt(2, Integer.parseInt(pid.getText()));
						ps.executeQuery();
					}
					if(!address.getText().trim().equals("")){
						ps = con
								.prepareStatement("UPDATE Patient SET address=? WHERE pid=?");
						ps.setString(1, address.getText());
						ps.setInt(2, Integer.parseInt(pid.getText()));
						ps.executeQuery();
					}
					if(!phone.getText().trim().equals("")){
						ps = con
								.prepareStatement("UPDATE Patient SET phone=? WHERE pid=?");
						ps.setString(1, phone.getText());
						ps.setInt(2, Integer.parseInt(pid.getText()));
						ps.executeQuery();
					}
					
					JOptionPane.showMessageDialog(null, "Update Patient Infor Successfully!");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		
	}
}
